﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using MySql.Data.MySqlClient;
using DateBaseConnectionPool;

namespace TestMySqlConnetPoll
{
    public struct objStruct
    {
        public DateTime time;
        public Int16 dev_no;
        public objTimeValue dValue;
    }
    public  class JyLlCyTable
    {
        #region 外部函数接口，绝缘、漏流、残压 操作，外部变量定义

        //public List<objDate> JyDailyReportTimeList = new List<objDate>();   //绝缘的日报表时间列表
        //public List<objDate> LlDailyReportTimeList = new List<objDate>();   //漏流的日报表时间列表
        //public List<objDate> CyDailyReportTimeList = new List<objDate>();   //残压的日报表时间列表

        //public List<objDate> JyMonthCurveTimeList = new List<objDate>();    //绝缘的月曲线时间列表
        //public List<objDate> LlMonthCurveTimeList = new List<objDate>();    //漏流的月曲线时间列表
        //public List<objDate> CyMonthCurveTimeList = new List<objDate>();    //残压的月曲线时间列表

        //public List<objDate> JyYearCurveTimeList = new List<objDate>();    //绝缘的年曲线时间列表
        //public List<objDate> LlYearCurveTimeList = new List<objDate>();    //漏流的年曲线时间列表
        //public List<objDate> CyYearCurveTimeList = new List<objDate>();    //残压的年曲线时间列表

        /// <summary>
        /// 设备类型号
        /// </summary>
        
        //以设备名称做键值
        public Dictionary<string, objStruct> Jy_NewstValueList = new Dictionary<string, objStruct>();  //绝缘最新值列表
        public Dictionary<string, objStruct> Ll_NewstValueList = new Dictionary<string, objStruct>();  //漏流最新值列表
        public Dictionary<string, objStruct> Cy_NewstValueList = new Dictionary<string, objStruct>();  //残压最新值列表

        public Dictionary<string, objStruct> JyValueDailyReportList = new Dictionary<string, objStruct>(); //绝缘的值日报表
        public Dictionary<string, objStruct> LlValueDailyReportList = new Dictionary<string, objStruct>(); //漏流的值日报表
        public Dictionary<string, objStruct> CyValueDailyReportList = new Dictionary<string, objStruct>(); //残压的值日报表

        public Dictionary<string, List<objTimeValue>> JyValueMonthCurveList = new Dictionary<string, List<objTimeValue>>(); //绝缘的月曲线
        public Dictionary<string, List<objTimeValue>> LlValueMonthCurveList = new Dictionary<string, List<objTimeValue>>(); //漏流的月曲线
//        public Dictionary<string, List<objAnalogDev>> CyValueMonthCurveList = new Dictionary<string, List<objAnalogDev>>(); //残压的月曲线

        public Dictionary<string, List<objTimeValue>> JyValueYearCurveList = new Dictionary<string, List<objTimeValue>>(); //绝缘的值年曲线
        public Dictionary<string, List<objTimeValue>> LlValueYearCurveList = new Dictionary<string, List<objTimeValue>>(); //漏流的值年曲线
//        public Dictionary<string, List<objAnalogDev>> CyValueYearCurveList = new Dictionary<string, List<objAnalogDev>>(); //残压的值年曲线

        //public bool Ready_JyTimeList = false;   //绝缘漏流残压的时间列表准备号标志。
        //public bool Ready_LlTimeList = false;
        //public bool Ready_CyTimeList = false;

        public bool Ready_JyDailyReport = false;
        public bool Ready_JyMonthCurve = false;
        public bool Ready_JyYearCurve = false;

        public bool Ready_LlDailyReport = false;
        public bool Ready_LlMonthCurve = false;
        public bool Ready_LlYearCurve = false;

        public bool Ready_CyDailyReport = false;
        //public bool Ready_CyMonthCurve = false;
        //public bool Ready_CyYearCurve = false;

        public bool Ready_JyNewstValue = false;
        public bool Ready_LlNewstValue = false;
        public bool Ready_CyNewstValue = false;

        public int ErrorFlag = 0;

        private Mutex keyMutex = new Mutex();
        static private int keykey = 0;
        /// <summary>
        /// 读取绝缘，漏流，残压的时间列表.，同时读取了日报表、月曲线、年曲线的时间列表
        /// </summary>
        /// <param name="CsmType"></param>
        //public void ReadJyLlCyTimeList(CSM_DevType CsmType)
        //{
        //    if (CsmType == CSM_DevType.残压) Ready_CyTimeList = false;
        //    else if (CsmType == CSM_DevType.漏流) Ready_LlTimeList = false;
        //    else if (CsmType == CSM_DevType.绝缘) Ready_JyTimeList = false;
        //    JyLlCyThreadStruct obj = new JyLlCyThreadStruct();
        //    obj.TableName = "jy$ll$cy";
        //    obj.CsmType = CsmType;
        //    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadReadJyLyTimeList), obj);
        //}

        public void ReadDailyReportValue(string DevTypeName, objDate date)
        {
            ErrorFlag = 0;
            if (DevTypeName.Contains("绝缘"))
            {
                Ready_JyDailyReport = false;
                JyValueDailyReportList.Clear();
            }
            else if (DevTypeName.Contains("漏流"))
            {
                Ready_LlDailyReport = false;
                LlValueDailyReportList.Clear();
            }
            else if (DevTypeName.Contains("残压"))
            {
                Ready_CyDailyReport = false;
                CyValueDailyReportList.Clear();
            }
            JyLlCyThreadStruct obj = new JyLlCyThreadStruct();
            obj.TableName = "jy$ll$cy";
            obj.CsmType = DevTypeName;
            obj.date = date;
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadReadDailyReportValueList), obj);
        }

        public void ReadMonthCurveValue(string DevTypeName, string DevName, string AttriName, int Year, int Month)
        {
            //CSM_DevType CsmType, AnalogCodeType AnalogType, Int16 dev_no, int Year, int Month
            ErrorFlag = 0;
            if (DevTypeName.Contains("绝缘"))
            {
                Ready_JyMonthCurve = false;
                JyValueMonthCurveList.Clear();
            }
            else if (DevTypeName.Contains("漏流"))
            {
                Ready_LlMonthCurve = false;
                LlValueMonthCurveList.Clear();
            }

            JyLlCyThreadStruct obj = new JyLlCyThreadStruct();
            obj.TableName = "jy$ll$cy";
            obj.CsmType = DevTypeName;
            obj.DevName = DevName;
            obj.AnalogType = c_Device.GetAnalogTypeFromName(AttriName);
            obj.date.year = Year;
            obj.date.month = (byte)Month;
            AnalogObj o = c_Device.GetAnalogDevNo(DevTypeName, DevName, AttriName);
            obj.devNo = o.devNo;
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadReadMonthCurveValueList), obj);
        }

        public void ReadYearCurveValue(string DevTypeName, string DevName, string AttriName, int Year)
        {
            ErrorFlag = 0;
            if (DevTypeName.Contains("绝缘"))
            {
                Ready_JyYearCurve = false;
                JyValueYearCurveList.Clear();
            }
            else if (DevTypeName.Contains("漏流"))
            {
                Ready_LlYearCurve = false;
                LlValueYearCurveList.Clear();
            }
            JyLlCyThreadStruct obj = new JyLlCyThreadStruct();
            obj.TableName = "jy$ll$cy";
            obj.CsmType = DevTypeName;
            obj.DevName = DevName;
            obj.AnalogType = c_Device.GetAnalogTypeFromName(AttriName);
            obj.date.year = Year;
            AnalogObj o = c_Device.GetAnalogDevNo(DevTypeName, DevName, AttriName);
            obj.devNo = o.devNo;
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadReadYearCurveValueList), obj);
        }

        public void ReadJyLlCyNewstValue(string DevTypeName)
        {
            ErrorFlag = 0;
            if (DevTypeName.Contains("绝缘"))
            {
                Ready_JyDailyReport = false;
                Jy_NewstValueList.Clear();
            }
            else if (DevTypeName.Contains("漏流"))
            {
                Ready_LlDailyReport = false;
                Ll_NewstValueList.Clear();
            }
            else if (DevTypeName.Contains("残压"))
            {
                Ready_CyNewstValue = false;
                Cy_NewstValueList.Clear();
            }

            
            JyLlCyThreadStruct obj = new JyLlCyThreadStruct();
            obj.TableName = "Newest_jy$ll$cy";
            obj.CsmType = DevTypeName;
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadReadNewstValueList), obj);
        }


        #region   绝缘，漏流，残压表操作

        public struct JyLlCyThreadStruct
        {
            public string TableName;
            public string CsmType;
            public string DevName;
            public AnalogCodeType AnalogType;
            public objDate date;
            public int devNo;
        }
        /// <summary>
        /// 往绝缘、漏流、残压表中插入数据
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="u_time"></param>
        /// <param name="type"></param>
        /// <param name="Dev_no"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool InsertJy_Ly_Cy_Data(Int32 u_time, CSM_DevType devtype, AnalogCodeType codetype, Int16 Dev_no, Int16 value)
        {
            bool tmpFlag = false;
            string TableName = "jy$ll$cy";
            if (TableName == "") return false;
            if (MySql.c != null && MySql.c.State == PoolState.Run)
            {
                MySqlConnection con = new MySqlConnection();
                string key = TableName + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Interlocked.Increment(ref keykey).ToString();
                try
                {
                    con = (MySqlConnection)MySql.c.GetConnectionFormPool(key);
                }
                catch (MySqlException ex)
                {
                    //写异常日志
                    tmpFlag = false;
                }
                
                if (con != null)
                {
                    StringBuilder DelStatement = new StringBuilder();
                    DelStatement.Append(string.Format("delete from {0} where year = {1} and month = {2} and day= {3} and type = {4} and codetype = {5} and dev_no = {6}",
                                                    TableName, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, (byte)devtype, (byte)codetype, Dev_no));
                    MySqlCommand delcommand = new MySqlCommand(DelStatement.ToString(),con);
                    try
                    {
                        delcommand.ExecuteNonQuery();
                        //插入前先删除一天相同的数据，一天只保留一个测试值。
                    }
                    catch (MySqlException e)
                    {

                    }

                    //StringBuilder updateStatement = new StringBuilder();
                    //updateStatement.Append(string.Format("update {0} set u_time = {1} and value = {2}",
                    //                                             TableName, u_time, value));
                    //updateStatement.Append(string.Format(" where year = {0} and month = {1} and day= {2} and type = {3} and codetype = {4} and dev_no = {5}"
                    //                                  , DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, (byte)devtype, (byte)codetype, Dev_no));

                    //MySqlCommand updatecommand = new MySqlCommand(updateStatement.ToString(), con);
                    //try
                    //{
                    //    updatecommand.ExecuteNonQuery();
                    //    tmpFlag = true;
                    //}
                    //catch (MySqlException e)
                    //{
                    //    //写异常日记
                    //    tmpFlag = false;       //写入失败
                    //}
                    StringBuilder InsertStatement = new StringBuilder();
                    InsertStatement.Append(string.Format("insert into {0} (year,month,day,u_time,type,codetype,dev_no,value)values(?year,?month,?day,?u_time,?type,?codetype,?Dev_no,?value)", TableName));
                    MySqlCommand InsertCommand = new MySqlCommand(InsertStatement.ToString(), con);
                    InsertCommand.Parameters.Add(new MySqlParameter("?year", MySqlDbType.UInt32)).Value = DateTime.Now.Year;
                    InsertCommand.Parameters.Add(new MySqlParameter("?month", MySqlDbType.UByte)).Value = (byte)DateTime.Now.Month;
                    InsertCommand.Parameters.Add(new MySqlParameter("?day", MySqlDbType.UByte)).Value = (byte)DateTime.Now.Day;
                    InsertCommand.Parameters.Add(new MySqlParameter("?u_time", MySqlDbType.UInt32)).Value = u_time;
                    InsertCommand.Parameters.Add(new MySqlParameter("?type", MySqlDbType.UByte)).Value = (byte)devtype;
                    InsertCommand.Parameters.Add(new MySqlParameter("?codetype", MySqlDbType.UByte)).Value = (byte)codetype;
                    InsertCommand.Parameters.Add(new MySqlParameter("?Dev_no", MySqlDbType.UInt16)).Value = Dev_no;
                    InsertCommand.Parameters.Add(new MySqlParameter("?value", MySqlDbType.UInt16)).Value = value;
                    try
                    {
                        int k = InsertCommand.ExecuteNonQuery();
                        if (k == 1) tmpFlag = true;        //写入成功
                        else tmpFlag = false;              //写入失败
                    }
                    catch (MySqlException e)
                    {
                        //写异常日记
                        tmpFlag = false;       //写入失败
                    }
                    try
                    {
                        MySql.c.DisposeConnection(key);
                    }
                    catch (MySqlException e)
                    {
                        //写异常日记
                    }
                }
            }
            return tmpFlag;
        }
        /// <summary>
        /// 把日期加入到日期列表内，需要过滤掉相同的日期,同一时刻，也更新了月曲线、年曲线时间表
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <param name="type"></param>
        //private void AddToJyLyCyDailyReportTimeList(int year, byte month, byte day, CSM_DevType devtype)
        //{
        //    objDate tmp = new objDate();
        //    tmp.year = year;
        //    tmp.month = month;
        //    tmp.day = day;
        //    if (devtype == CSM_DevType.绝缘)  //绝缘
        //    {
        //        if (JyDailyReportTimeList.Contains(tmp)) { }
        //        else JyDailyReportTimeList.Add(tmp);
        //        tmp.day = 1;
        //        if (JyMonthCurveTimeList.Contains(tmp)) { }
        //        else JyMonthCurveTimeList.Add(tmp);
        //        tmp.month = 1;
        //        if (JyYearCurveTimeList.Contains(tmp)) { }
        //        else JyYearCurveTimeList.Add(tmp);
        //    }
        //    else if (devtype == CSM_DevType.漏流)
        //    {
        //        if (LlDailyReportTimeList.Contains(tmp)) { }
        //        else LlDailyReportTimeList.Add(tmp);
        //        tmp.day = 1;
        //        if (LlMonthCurveTimeList.Contains(tmp)) { }
        //        else LlMonthCurveTimeList.Add(tmp);
        //        tmp.month = 1;
        //        if (LlYearCurveTimeList.Contains(tmp)) { }
        //        else LlYearCurveTimeList.Add(tmp);
        //    }
        //    else if (devtype == CSM_DevType.残压)
        //    {
        //        if (CyDailyReportTimeList.Contains(tmp)) { }
        //        else CyDailyReportTimeList.Add(tmp);
        //        tmp.day = 1;
        //        if (CyMonthCurveTimeList.Contains(tmp)) { }
        //        else CyMonthCurveTimeList.Add(tmp);
        //        tmp.month = 1;
        //        if (CyYearCurveTimeList.Contains(tmp)) { }
        //        else CyYearCurveTimeList.Add(tmp);
        //    }
        //}
        /// <summary>
        /// 读取绝缘、漏流、残压的时间列表
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="type"></param>
        /// <returns></returns>      
        //private void ThreadReadJyLyTimeList(object o)
        //{
        //    JyLlCyThreadStruct obj = new JyLlCyThreadStruct();
        //    obj = (JyLlCyThreadStruct)o;
        //    string TableName = obj.TableName;
        //    CSM_DevType type = obj.CsmType;

        //    if (type == CSM_DevType.绝缘)
        //    {
        //        JyDailyReportTimeList.Clear();
        //        JyMonthCurveTimeList.Clear();
        //        JyYearCurveTimeList.Clear();
        //    }
        //    else if (type == CSM_DevType.漏流)
        //    {
        //        LlDailyReportTimeList.Clear();
        //        LlMonthCurveTimeList.Clear();
        //        LlYearCurveTimeList.Clear();
        //    }
        //    else if (type == CSM_DevType.残压)
        //    {
        //        CyDailyReportTimeList.Clear();
        //        CyMonthCurveTimeList.Clear();
        //        CyYearCurveTimeList.Clear();
        //    }
        //    if (MySql.c != null && MySql.c.State == PoolState.Run)
        //    {
        //        MySqlConnection con = new MySqlConnection();
        //        string key = TableName + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString()+ (MySql.key++).ToString();
        //        try
        //        {
        //            con = (MySqlConnection)MySql.c.GetConnectionFormPool(key);
        //        }
        //        catch (MySqlException ex)
        //        {
        //            //写异常日志
        //        }
        //        if (con != null)
        //        {
        //            StringBuilder SelectStatement = new StringBuilder();

        //            SelectStatement.Append(string.Format("select(year,month,day) from{0} where type = {1}", TableName, type));

        //            try
        //            {
        //                MySqlCommand selectCommand = new MySqlCommand(SelectStatement.ToString(), con);
        //                selectCommand.CommandTimeout = 120;   //120s操作超时

        //                MySqlDataReader read = selectCommand.ExecuteReader();
        //                while (read.Read())
        //                {
        //                    int year = read.GetInt32(0);
        //                    byte month = read.GetByte(1);
        //                    byte day = read.GetByte(2);
        //                    AddToJyLyCyDailyReportTimeList(year, month, day, type);
        //                }
        //                read.Close();
        //                if (type == CSM_DevType.残压) Ready_CyTimeList = true;
        //                else if (type == CSM_DevType.漏流) Ready_LlTimeList = true;
        //                else if (type == CSM_DevType.绝缘) Ready_JyTimeList = true;
        //            }
        //            catch (MySqlException e)
        //            {
        //                //写错误日志                        
        //            }
        //            try
        //            {
        //                MySql.c.DisposeConnection(key);
        //            }
        //            catch (MySqlException e)
        //            {
        //                //写错误日志                       
        //            }
        //        }
        //    }
        //    return;
        //}


        /// <summary>
        /// 把读出的绝缘漏流，残压值加入按照设备类型和设备分类的列表中
        /// </summary>
        /// <param name="u_time"></param>
        /// <param name="dev_type"></param>
        /// <param name="dev_no"></param>
        /// <param name="value"></param>
        /// <param name="type"></param>
        private void AddToDailyReportValueList(int u_time, Int16 dev_no, Int16 value, string TypeName, AnalogCodeType codetype)
        {
            objTimeValue TmpValue = new objTimeValue();

            string AnalogName = c_Device.GetAnalogTypeName(codetype);
            ObjValue vt = c_RealTimeValue.ConvertAnalogValue(AnalogName, value);
            TmpValue.Tvalue = vt;
            TmpValue.Dvalue = value;
            TmpValue.u_time = u_time;

            objStruct t = new objStruct();
            t.dev_no = dev_no;
            t.time = MySql.Time_tToDateTime(u_time);
            t.dValue = TmpValue;

            string DevName = c_Device.GetJyLlCyDevName(TypeName, dev_no);
                        
            string codeName = c_Device.GetAnalogTypeName(codetype);

            if (TypeName.Contains("绝缘"))
            {
                if (JyValueDailyReportList.ContainsKey(DevName))
                {                    
                    
                }
                else
                {
                    JyValueDailyReportList.Add(DevName, t);
                }
            }
            else if (TypeName.Contains("漏流"))
            {
                if (LlValueDailyReportList.ContainsKey(DevName))
                {
                    
                }
                else
                {
                    LlValueDailyReportList.Add(DevName, t);
                }
            }
            else if (TypeName.Contains("残压"))
            {
                if (CyValueDailyReportList.ContainsKey(DevName))
                {
                 
                }
                else
                {
                    CyValueDailyReportList.Add(DevName, t);
                }
            }
        }
        /// <summary>
        /// 查询绝缘、漏流、残压的日报表
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private void ThreadReadDailyReportValueList(object o)
        {
            JyLlCyThreadStruct obj = new JyLlCyThreadStruct();
            obj = (JyLlCyThreadStruct)o;
            string TableName = obj.TableName;
            int year = obj.date.year;
            byte month = obj.date.month;
            byte day = obj.date.day;
            string devTypeName = obj.CsmType;

            int type = 0;

            if (devTypeName.Contains("绝缘")) type = (byte)CSM_DevType.绝缘;
            else if (devTypeName.Contains("漏流")) type = (byte)CSM_DevType.漏流;
            else if (devTypeName.Contains("残压")) type = (byte)CSM_DevType.残压;
            if (TableName == "")
            {
                ErrorFlag = 1;
                return;
            }
            if (MySql.c != null && MySql.c.State == PoolState.Run)
            {
                MySqlConnection con = new MySqlConnection();
                string key = TableName + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Interlocked.Increment(ref keykey).ToString();
                try
                {
                    con = (MySqlConnection)MySql.c.GetConnectionFormPool(key);
                }
                catch (MySqlException ex)
                {
                    //写异常日志
                    ErrorFlag = 2;
                }
                if (con != null)
                {
                    StringBuilder SelectStatement = new StringBuilder();

                    SelectStatement.Append(string.Format("select u_time,codetype,dev_no,value  from {0} where year={1} and month ={2} and day ={3} and type = {4}",
                                                          TableName, year, month, day, type));

                    try
                    {
                        MySqlCommand selectCommand = new MySqlCommand(SelectStatement.ToString(), con);
                        selectCommand.CommandTimeout = 120;   //120s操作超时

                        MySqlDataReader read = selectCommand.ExecuteReader();
                        while (read.Read())
                        {
                            int u_time = read.GetInt32(read.GetOrdinal("u_time"));
                            byte codetype = read.GetByte(read.GetOrdinal("codetype"));
                            Int16 dev_no = read.GetInt16(read.GetOrdinal("dev_no"));
                            Int16 value = read.GetInt16(read.GetOrdinal("value"));
                            AddToDailyReportValueList(u_time, dev_no, value, devTypeName, (AnalogCodeType)codetype);
                        }
                        read.Close();
                        if (devTypeName.Contains("绝缘")) Ready_CyDailyReport = true;
                        else if (devTypeName.Contains("漏流")) Ready_LlDailyReport = true;
                        else if (devTypeName.Contains("残压")) Ready_JyDailyReport = true;
                    }
                    catch (MySqlException e)
                    {
                        ErrorFlag = 3;
                    }
                    try
                    {
                        MySql.c.DisposeConnection(key);
                    }
                    catch (MySqlException e)
                    {
                        ErrorFlag = 4;
                    }
                }
                else ErrorFlag = 6;
            }
            else ErrorFlag = 7;
        }

        private void AddToMonthCurveValueList(int u_time,string DevName, Int16 dev_no, Int16 value, string devtypeName, AnalogCodeType codetype)
        {
            objTimeValue TmpValue = new objTimeValue();
            TmpValue.u_time = u_time;

            string AnalogName = c_Device.GetAnalogTypeName(codetype);
            ObjValue vt = c_RealTimeValue.ConvertAnalogValue(AnalogName, value);
            TmpValue.Tvalue = vt;
            TmpValue.Dvalue = value;

            List<objTimeValue> k = new List<objTimeValue>();
            k.Add(TmpValue);

            if (devtypeName.Contains("绝缘"))
            {
                if (JyValueMonthCurveList.ContainsKey(DevName))
                {
                    JyValueMonthCurveList[DevName].Add(TmpValue);
                }
                else
                {
                    JyValueMonthCurveList.Add(DevName, k);
                }
            }
            else if (devtypeName.Contains("漏流"))
            {
                if (LlValueMonthCurveList.ContainsKey(DevName))
                {
                    LlValueMonthCurveList[DevName].Add(TmpValue);
                }
                else
                {
                    LlValueMonthCurveList.Add(DevName, k);
                }
            }
        }

        private void ThreadReadMonthCurveValueList(object o)
        {
            JyLlCyThreadStruct obj = new JyLlCyThreadStruct();
            obj = (JyLlCyThreadStruct)o;
            string TableName = obj.TableName;
            int year = obj.date.year;
            byte month = obj.date.month;
            string devtype = obj.CsmType;
            string devName = obj.DevName;
            AnalogCodeType AnalogType = obj.AnalogType;
            int devNo = obj.devNo;
            int type = 0;
            if (devtype.Contains("绝缘")) type = (byte)CSM_DevType.绝缘;
            else if(devtype.Contains("漏流")) type = (byte)CSM_DevType.漏流;

            if (TableName == "")
            {
                ErrorFlag = 1;
                return; 
            }

            if (MySql.c != null && MySql.c.State == PoolState.Run)
            {
                MySqlConnection con = new MySqlConnection();
                string key = TableName + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Interlocked.Increment(ref keykey).ToString();
                try
                {
                    con = (MySqlConnection)MySql.c.GetConnectionFormPool(key);
                }
                catch (MySqlException ex)
                {
                    //写异常日志
                    ErrorFlag = 2;
                }
                if (con != null)
                {
                    StringBuilder SelectStatement = new StringBuilder();

                    SelectStatement.Append(string.Format("select u_time,value from {0} where year={1} and month ={2} and type = {3} and codetype = {4} and dev_no = {5}",
                                                          TableName, year, month, type, (byte)AnalogType, devNo));
                    try
                    {
                        MySqlCommand selectCommand = new MySqlCommand(SelectStatement.ToString(), con);
                        selectCommand.CommandTimeout = 120;   //120s操作超时

                        MySqlDataReader read = selectCommand.ExecuteReader();
                        while (read.Read())
                        {
                            int u_time = read.GetInt32(read.GetOrdinal("u_time"));
                            Int16 value = read.GetInt16(read.GetOrdinal("value"));
                            AddToMonthCurveValueList(u_time, devName, (Int16)devNo, value, devtype, AnalogType);
                        }
                        read.Close();
                        if (devtype.Contains("绝缘")) Ready_JyMonthCurve = true;
                        else if (devtype.Contains("漏流")) Ready_LlMonthCurve = true;
                    }
                    catch (MySqlException e)
                    {
                        ErrorFlag = 3;
                    }
                    try
                    {
                        MySql.c.DisposeConnection(key);
                    }
                    catch (MySqlException e)
                    {
                        ErrorFlag = 4;
                    }
                }
                else ErrorFlag = 6;
            }
            else ErrorFlag = 7;
        }

        private void AddToYearCurveValueList(int u_time, string DevName, Int16 dev_no, Int16 value, string devtypeName, AnalogCodeType codetype)
        {
            objTimeValue TmpValue = new objTimeValue();
            TmpValue.u_time = u_time;

            string AnalogName = c_Device.GetAnalogTypeName(codetype);
            ObjValue vt = c_RealTimeValue.ConvertAnalogValue(AnalogName, value);
            TmpValue.Tvalue = vt;
            TmpValue.Dvalue = value;
            

            List<objTimeValue> k = new List<objTimeValue>();
            k.Add(TmpValue);

            if (devtypeName.Contains("绝缘"))
            {
                if (JyValueYearCurveList.ContainsKey(DevName))
                {
                    JyValueYearCurveList[DevName].Add(TmpValue);
                }
                else
                {
                    JyValueYearCurveList.Add(DevName, k);
                }
            }
            else if (devtypeName.Contains("漏流"))
            {
                if (LlValueYearCurveList.ContainsKey(DevName))
                {
                    LlValueYearCurveList[DevName].Add(TmpValue);
                }
                else
                {
                    LlValueYearCurveList.Add(DevName, k);
                }
            }
        }
        private void ThreadReadYearCurveValueList(object o)
        {
            JyLlCyThreadStruct obj = new JyLlCyThreadStruct();
            obj = (JyLlCyThreadStruct)o;
            string TableName = obj.TableName;
            int year = obj.date.year;
            string devtype = obj.CsmType;
            string DevName = obj.DevName;
            AnalogCodeType AnalogType = obj.AnalogType;
            int devNo = obj.devNo;
            int type = 0;
            if (devtype.Contains("绝缘")) type = (byte)CSM_DevType.绝缘;
            else if (devtype.Contains("漏流")) type = (byte)CSM_DevType.漏流;
            if (TableName == "")
            {
                ErrorFlag = 1;
                return;
            }
            if (MySql.c != null && MySql.c.State == PoolState.Run)
            {
                MySqlConnection con = new MySqlConnection();
                string key = TableName + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Interlocked.Increment(ref keykey).ToString();
                try
                {
                    con = (MySqlConnection)MySql.c.GetConnectionFormPool(key);
                }
                catch (MySqlException ex)
                {
                    //写异常日志
                    ErrorFlag = 2;
                }
                if (con != null)
                {
                    StringBuilder SelectStatement = new StringBuilder();

                    SelectStatement.Append(string.Format("select u_time,value from {0} where year={1} and type = {2} and codetype = {3} and dev_no = {4}",
                                                          TableName, year, type, (byte)AnalogType, devNo));
                    try
                    {
                        MySqlCommand selectCommand = new MySqlCommand(SelectStatement.ToString(), con);
                        selectCommand.CommandTimeout = 120;   //120s操作超时

                        MySqlDataReader read = selectCommand.ExecuteReader();
                        while (read.Read())
                        {
                            int u_time = read.GetInt32(read.GetOrdinal("u_time"));
                            Int16 value = read.GetInt16(read.GetOrdinal("value"));
                            AddToYearCurveValueList(u_time, DevName, (Int16)devNo, value, devtype, AnalogType);
                        }
                        read.Close();
                        if (devtype.Contains("绝缘")) Ready_JyYearCurve = true;
                        else if (devtype.Contains("漏流")) Ready_LlYearCurve = true;
                    }
                    catch (MySqlException e)
                    {
                        ErrorFlag = 5;
                    }
                    try
                    {
                        MySql.c.DisposeConnection(key);
                    }
                    catch (MySqlException e)
                    {
                        ErrorFlag = 4;
                    }
                }
                else ErrorFlag = 6;
            }
            else ErrorFlag = 7;
        }
        #endregion

        #region  绝缘、漏流、残压最新值操作
        /// <summary>
        /// 把数据插入绝缘、漏流、残压最新表格中
        /// 首先需要找到表中该设备的值，删除后，再进行更新
        /// </summary>
        /// <param name="type"></param>
        /// <param name="dev_type"></param>
        /// <param name="dev_no"></param>
        /// <param name="u_time"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool InsertToNewstJyLlCyTable(int u_time, CSM_DevType devtype, AnalogCodeType codetype, Int16 dev_no, Int16 value)
        {
            bool tmpFlag = false;
            string TableName = "newest_jy$ll$cy";
            if (TableName == "") return tmpFlag;

            if (MySql.c != null && MySql.c.State == PoolState.Run)
            {
                MySqlConnection con = new MySqlConnection();
                string key = TableName + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Interlocked.Increment(ref keykey).ToString();
                try
                {
                    con = (MySqlConnection)MySql.c.GetConnectionFormPool(key);
                }
                catch (MySqlException ex)
                {
                    //写异常日志
                    tmpFlag = false;
                }
                if (con != null)
                {
                    StringBuilder DelStatement = new StringBuilder();
                    DelStatement.Append(string.Format("delete from {0} where type = {1} and codetype = {2} and dev_no = {3}", TableName, (byte)devtype, (byte)codetype, dev_no));
                    MySqlCommand delCommand = new MySqlCommand(DelStatement.ToString(), con);
                    delCommand.ExecuteNonQuery();

                    StringBuilder InsertStatement = new StringBuilder();
                    InsertStatement.Append(string.Format("insert into {0}(u_time,type,codetype,dev_no,value)values(?u_time,?type,?codetype,?dev_no,?value)", TableName));

                    MySqlCommand InsertCommand = new MySqlCommand(InsertStatement.ToString(), con);

                    InsertCommand.Parameters.Add(new MySqlParameter("?u_time", MySqlDbType.UInt32)).Value = u_time;
                    InsertCommand.Parameters.Add(new MySqlParameter("?type", MySqlDbType.UByte)).Value = (byte)devtype;
                    InsertCommand.Parameters.Add(new MySqlParameter("?codetype", MySqlDbType.UByte)).Value = (byte)codetype;
                    InsertCommand.Parameters.Add(new MySqlParameter("?dev_no", MySqlDbType.UInt16)).Value = dev_no;
                    InsertCommand.Parameters.Add(new MySqlParameter("?value", MySqlDbType.UInt16)).Value = value;

                    try
                    {
                        int k = InsertCommand.ExecuteNonQuery();
                        if (k == 1) tmpFlag = true;        //写入成功
                        else tmpFlag = false;              //写入失败
                    }
                    catch (MySqlException e)
                    {
                        //写异常日记
                        tmpFlag = false;       //写入失败
                    }
                    try
                    {
                        MySql.c.DisposeConnection(key);
                    }
                    catch (MySqlException e)
                    {
                        //写异常日记
                    }
                }
            }
            return tmpFlag;
        }
        /// <summary>
        /// 把从数据库中读出的最新值加入到list列表中
        /// </summary>
        /// <param name="type"></param>
        /// <param name="dev_type"></param>
        /// <param name="Dev_no"></param>
        /// <param name="u_time"></param>
        /// <param name="value"></param>
        private void AddToNewstValueList(string csmName, AnalogCodeType codetype, Int16 Dev_no, int u_time, Int16 value)
        {
            string AnalogName = c_Device.GetAnalogTypeName(codetype);
            ObjValue tv = c_RealTimeValue.ConvertAnalogValue(AnalogName, value);

            string DevName = c_Device.GetJyLlCyDevName(csmName, Dev_no);
            objStruct t = new objStruct();
            t.dev_no = Dev_no;
            t.time = MySql.Time_tToDateTime(u_time);
            
            t.dValue.u_time = u_time;
            t.dValue.Dvalue = value;
            t.dValue.Tvalue = tv;

            try
            {
                if (csmName.Contains("绝缘"))
                {
                    Jy_NewstValueList.Add(DevName, t);
                }
                else if (csmName.Contains("漏流"))
                {
                    Ll_NewstValueList.Add(DevName, t);
                }
                else if (csmName.Contains("残压"))
                {
                    Cy_NewstValueList.Add(DevName, t);
                }
            }
            catch
            {
                Console.WriteLine("加入绝缘漏流残压值错误");
            }
        }
        /// <summary>
        /// 读取绝缘、漏流、残压最新值
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private void ThreadReadNewstValueList(object o)
        {
            JyLlCyThreadStruct obj = new JyLlCyThreadStruct();
            obj = (JyLlCyThreadStruct)o;

            string TableName = obj.TableName;
            string CsmName = obj.CsmType;

            int type = 0;

            if (CsmName.Contains("绝缘")) type = (byte)CSM_DevType.绝缘;
            else if (CsmName.Contains("漏流")) type = (byte)CSM_DevType.漏流;
            else if (CsmName.Contains("残压")) type = (byte)CSM_DevType.残压;

            if (TableName == "")
            {
                ErrorFlag = 1;
                return;
            }
            if (MySql.c != null && MySql.c.State == PoolState.Run)
            {
                MySqlConnection con = new MySqlConnection();
                string key = TableName + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Interlocked.Increment(ref keykey).ToString();
                try
                {
                    con = (MySqlConnection)MySql.c.GetConnectionFormPool(key);
                }
                catch (MySqlException ex)
                {
                    //写异常日志
                    ErrorFlag = 2;
                }
                if (con != null)
                {
                    StringBuilder SelectStatement = new StringBuilder();

                    SelectStatement.Append(string.Format("select u_time,codetype,dev_no,value from {0} where type = {1}", TableName, type));

                    try
                    {
                        MySqlCommand selectCommand = new MySqlCommand(SelectStatement.ToString(), con);
                        selectCommand.CommandTimeout = 120;   //120s操作超时

                        MySqlDataReader read = selectCommand.ExecuteReader();
                        while (read.Read())
                        {
                            int u_time = read.GetInt32(read.GetOrdinal("u_time"));
                            AnalogCodeType codetype = (AnalogCodeType)read.GetByte(read.GetOrdinal("codetype"));
                            Int16 dev_no = read.GetInt16(read.GetOrdinal("dev_no"));
                            Int16 value = read.GetInt16(read.GetOrdinal("value"));
                            AddToNewstValueList(CsmName, codetype, dev_no, u_time, value);
                        }
                        read.Close();
                        if (CsmName.Contains("绝缘")) Ready_JyNewstValue = true;
                        else if (CsmName.Contains("漏流")) Ready_LlNewstValue = true;
                        else if (CsmName.Contains("残压")) Ready_CyNewstValue = true;
                    }
                    catch (MySqlException e)
                    {
                        ErrorFlag = 3;
                    }
                    try
                    {
                        MySql.c.DisposeConnection(key);
                    }
                    catch (MySqlException e)
                    {
                        ErrorFlag = 4;
                    }
                }
                else ErrorFlag = 6;
            }
            else ErrorFlag = 7;
        }
        #endregion

        #endregion
    }
}
